﻿using ACS.SPiiPlusNET;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Hardware.Acs
{
    public interface IAcsController
    {
        //#region 委托
        ///// <summary>
        ///// Acs连接事件
        ///// </summary>
        //public delegate void AcsConnectingEvent();
        ///// <summary>
        ///// Acs断开连接事件
        ///// </summary>
        //public delegate void AcsDisconnectingEvent();
        //#endregion
        //#region 事件
        //public event AcsConnectingEvent AcsConnecting;
        //public event AcsDisconnectingEvent AcsDisconnecting;
        //#endregion
        #region 属性
        /// <summary>
        /// AcsApi实例
        /// </summary>
        public Api AcsInstance { get; }
        /// <summary>
        /// 连接控制器IP地址
        /// </summary>
        public string IpAddress { get; set; }
        /// <summary>
        /// 连接控制器端口号
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// Acs是否已经连接
        /// </summary>
        public bool IsConnected { get; }
        /// <summary>
        /// 控制器轴的数量
        /// </summary>
        public double AxesCount { get; }
        /// <summary>
        /// Buffer数量
        /// </summary>
        public double BuffersCount { get; }
        #endregion
        #region AcsApi
        /// <summary>
        /// 连接到EthernetTCP
        /// </summary>
        public void ConnectToEthernetTCP();
        /// <summary>
        /// 连接到EthernetTCP
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        public void ConnectToEthernetTCP(string address, int port);
        /// <summary>
        /// 连接到虚拟仿真
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        public void ConnectToSimulator();
        /// <summary>
        /// 轴使能
        /// </summary>
        /// <param name="axis">目标轴类型</param>
        public void Enable(Axis axis);
        /// <summary>
        /// 轴断使能
        /// </summary>
        /// <param name="axis">目标轴类型</param>
        public void Disable(Axis axis);
        /// <summary>
        /// 使能Fault
        /// </summary>
        /// <param name="axis">目标轴类型</param>
        /// <param name="safetyControlMasks"></param>
        public void EnableFault(Axis axis, SafetyControlMasks safetyControlMasks);
        /// <summary>
        /// 禁用Fault
        /// </summary>
        /// <param name="axis">目标轴类型</param>
        /// <param name="safetyControlMasks"></param>
        public void DisableFault(Axis axis, SafetyControlMasks safetyControlMasks);
        /// <summary>
        /// Jog运动
        /// </summary>
        /// <param name="motionFlags">运动标志</param>
        /// <param name="axis">目标轴对象（ID）</param>
        /// <param name="velocity">速度</param>
        public void Jog(MotionFlags motionFlags, Axis axis, double velocity);
        /// <summary>
        /// PTP运动
        /// </summary>
        /// <param name="motionFlags"></param>
        /// <param name="axis"></param>
        /// <param name="position">目标位置</param>
        public void ToPoint(MotionFlags motionFlags, Axis axis, double position);
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="axis">目标轴对象</param>
        public void Halt(Axis axis);
        /// <summary>
        /// 写入变量
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="variable">变量名</param>
        public void WriteVariable(object value, string variable);
        /// <summary>
        /// 写入变量
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="variable">变量名</param>
        public void WriteVariable(object value, string variable, ProgramBuffer programBuffer);
        /// <summary>
        /// 通过Terminal发送指令
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public string SendCommand(string command);
        /// <summary>
        /// 断开连接
        /// </summary>
        public void Disconnect();
        /// <summary>
        /// 获取电机状态
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public MotorStates GetMotorStates(Axis axis);
        /// <summary>
        /// 目标轴是否回参完成
        /// </summary>
        /// <param name="axis">目标轴</param>
        public bool ReadAxisIsReferenced(Axis axis);
        /// <summary>
        /// 停止所有轴运动
        /// </summary>
        public void KillAllAxes();
        /// <summary>
        /// 获取电机错误码
        /// </summary>
        /// <param name="axis"></param>
        /// <returns>错误码</returns>
        public int GetMotorError(Axis axis);
        /// <summary>
        /// 获取错误码详细信息
        /// </summary>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public string GetErrorString(int errorCode);
        /// <summary>
        /// 获取指定MotorError
        /// </summary>
        /// <returns></returns>
        public bool GetFault(Axis axis);
        /// <summary>
        /// 重启控制器
        /// </summary>
        /// <param name="timeOut"></param>
        public void ControllerReboot(int timeOut);
        /// <summary>
        /// 重启控制器并恢复出厂设置
        /// </summary>
        /// <param name="timeOut"></param>
        public void ControllerRebootDefualt(int timeOut);
        /// <summary>
        /// 获取当前速度
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public double GetCurrentVelocity(Axis axis);
        /// <summary>
        /// 获取急停减速度
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public double GetKillDeceleration(Axis axis);
        /// <summary>
        /// 获取当前位置误差
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public double GetCurrentPositionError(Axis axis);
        /// <summary>
        /// 获取当前位置反馈
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public double GetCurrentFeedbackPosition(Axis axis);
        /// <summary>
        /// 获取当前回参位置
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>
        public double GetCurrentReferencePosition(Axis axis);
        /// <summary>
        /// 读变量
        /// </summary>
        /// <param name="variable"></param>
        /// <param name="programBuffer"></param>
        /// <param name="from1"></param>
        /// <param name="to1"></param>
        /// <param name="from2"></param>
        /// <param name="to2"></param>
        /// <returns></returns>
        public object ReadVariable(string variable, ProgramBuffer programBuffer = ProgramBuffer.ACSC_NONE, int from1 = -1, int to1 = -1, int from2 = -1, int to2 = -1);
        /// <summary>
        /// 写变量
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="variable">变量名</param>
        /// <param name="programBuffer"></param>
        /// <param name="from1"></param>
        /// <param name="to1"></param>
        /// <param name="from2"></param>
        /// <param name="to2"></param>
        public void WriteVariable(object value, string variable, ProgramBuffer programBuffer = ProgramBuffer.ACSC_NONE, int from1 = -1, int to1 = -1, int from2 = -1, int to2 = -1);
        /// <summary>
        /// 保存到Flash缓冲区
        /// </summary>
        public void SaveToFlash();
        /// <summary>
        /// 编译Buffer
        /// </summary>
        public void CompileBuffer(ProgramBuffer programBuffer);
        /// <summary>
        /// 暂停Buffer
        /// </summary>
        public void SuspendBuffer(ProgramBuffer programBuffer);
        /// <summary>
        /// 停止Buffer
        /// </summary>
        public void StopBuffer(ProgramBuffer programBuffer);
        /// <summary>
        /// 运行Buffer
        /// </summary>
        public void RunBuffer(ProgramBuffer programBuffer);
        /// <summary>
        /// 获取程序状态
        /// </summary>
        /// <param name="programBuffer"></param>
        /// <returns></returns>
        public ProgramStates GetProgramState(ProgramBuffer programBuffer);
        /// <summary>
        /// 获取程序错误代码
        /// </summary>
        /// <param name="programBuffer"></param>
        /// <returns></returns>
        public int GetProgramError(ProgramBuffer programBuffer);
        /// <summary>
        /// 取消注册急停组件
        /// </summary>
        public void UnregisterEmergencyStop();
        /// <summary>
        /// 注册急停组件
        /// </summary>
        public void RegisterEmergencyStop();
        #endregion
    }
}
